<template>
  <div>
    <div class="filter-container">
      <!-- <el-input v-model="listQuery.identifier" placeholder="编码" class="filter-item" /> -->
      <el-input v-model="listQuery.name$lk" placeholder="商品名称" class="filter-item" />
      <Cascader v-model="listQueryCategoryId" ref="listQueryCategoryId" placeholder="商品分类" :options="categoryData"
        :props="props" class="filter-item" style="width: 210px;" />
      <el-select v-model="listQuery.status" placeholder="状态" clearable class="filter-item">
        <el-option v-for="item in statusJson" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-cascader class="filter-item" style="width: 210px;" v-model="storeId" :options="storeList"
        :props="{ checkStrictly: true,value:'id',label:'name' }" clearable placeholder="仓位"></el-cascader>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter"
        >
        查询
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <!-- <el-table-column label="序号" type="index" align="center" width="60px" /> -->
      <el-table-column label="数量" align="center">
        <template slot-scope="{row}">
          <el-input-number v-model="row.num" :precision="0" :controls="false" @change="inputChange" style="width:100%">
          </el-input-number>
          <!-- <el-input v-model="row.num" @change="inputChange" /> -->
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="name" align="center" />
      <el-table-column label="商品分类" prop="categoryName" align="center" />
      <!-- <el-table-column label="编号" prop="identifier" align="center" /> -->
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <span>{{ returnValueToLable(row.status,statusJson) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="定价" prop="priceText" align="center" />
      <!-- <el-table-column label="销量" prop="saleNum" align="center" /> -->
      <el-table-column label="仓位" prop="storeName" align="center" />
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { categoryPageVoByQo, productPageByQo } from '@/api/product'
import { storePageByQo } from '@/api/store'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import Cascader from '@/components/Cascader'

export default {
  name: 'ProductList',
  directives: { waves },
  components: { Cascader, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        name$lk: '',
        identifier: '',
        categoryId: '',
        status: '',
        storeId: ''
      },
      multipleSelection: [],
      categoryData: [],
      props: { value: 'id', label: 'name' },
      listQueryCategoryId: '',
      statusJson: [
        { value: 0, label: '下架' },
        { value: 1, label: '上架' }
      ],
      storeList: [],
      storeId: ''
    }
  },
  watch: {
    listQueryCategoryId(val) {
      this.listQuery.categoryId = this.$refs['listQueryCategoryId'].getIds(val)
    },
    storeId(val) {
      this.listQuery.storeId = val && val.length ? val[val.length - 1] : ''
    }
  },
  created() {
    this.getList()
    this.getCategory()
    this.getStoreList()
  },
  methods: {
    getCategory() {
      categoryPageVoByQo().then((res) => {
        this.$set(this, 'categoryData', res || [])
      })
    },
    getStoreList() {
      storePageByQo().then((res) => {
        this.$set(this, 'storeList', res || [])
      })
    },
    resetNum() {
      this.multipleSelection = ''
      this.$emit('productSelectionChange', this.multipleSelection)
      if (this.list && this.list.length > 0) {
        this.list = this.list.map(element => {
          this.$set(element, 'num', '')
          return element
        });
      }
    },
    getList() {
      this.listLoading = true
      productPageByQo(this.listQuery).then(response => {
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
        this.resetNum()
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    // 批量操作
    handleSelectionChange(val) {
      this.multipleSelection = val.map((ele) => {
        return {
          id: ele.id,
          num: ele.num
        }
      })
      this.$emit('productSelectionChange', this.multipleSelection)
    },
    inputChange() {
      if (this.multipleSelection && this.multipleSelection.length > 0 && this.list && this.list.length > 0) {
        this.multipleSelection.forEach(element => {
          this.list.some(list => {
            if (element.id === list.id) {
              this.$set(element, 'num', list.num)
              return true
            }
          });
        });
        this.$emit('productSelectionChange', this.multipleSelection)
      }
    }
  }
}
</script>
